<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="fragments/easyui_insdep_head :: easyui_insdep_head" th:with="title='库存管理'">
</head>
<link th:href="@{/filter/Fliter.css}" rel="stylesheet"/>
<script type="text/javascript" th:src="@{/filter/Filter.js}"/>

<body>

<table id="tt" class="easyui-datagrid" style="width:100%;height:100%" method="get"
       toolbar="#ttb" pageList='[1,10,20,30,50]' singleSelect="true"
       rownumbers="true" pagination="true">
    <thead>
    <tr>
        <th field="wareBarCode" width="160">条形码</th>
        <th field="wareName" width="500">名称</th>
        <th field="repertoryName" width="100">仓库名</th>
        <th field="stockNum" width="50">库存数量</th>
    </tr>
    </thead>
    <div id="ttb" style="padding:3px">
        <div id="filter"></div>
        <span>仓库:</span>
        <input class="easyui-combobox"
              id="repertoryId"
               data-options="
					url:'/repertory/',
					method:'get',
					valueField:'repertoryId',
					textField:'repertoryName',
			" />
        <span>名称:</span>
        <input id="wareName" style="line-height:26px;border:1px solid #ccc"/>
        <span>条形码:</span>
        <input id="wareBarCode" style="line-height:26px;border:1px solid #ccc"/>
        <input id="componentId" type="hidden"/>
        <script type="text/javascript">
            //<![CDATA[
            var componentParamDetailIds = new Set();
            //]]>
        </script>
        <input type="checkbox" id="isFuzzy" checked="checked" />模糊查询
        <a href="#" class="easyui-linkbutton" plain="true" onclick="doSearch()">查找</a>
        <a href="#" class="easyui-linkbutton" plain="true" onclick="doReSet()">重置</a>

        <script type="text/javascript">
            //<![CDATA[
            $(function () {
                //单选
                $('#filter').comboboxfilter({
                    url: '/component/filter',
                    method: 'get',
                    text: "配件",
                    onSelect: function (itemData) {
                        $("#componentId").val(itemData.id);
                        doSearch();
                        // 获取他的属性
                        $.get("/componentParameter/", {
                            compId: itemData.id
                        }, function (data) {
                            if (typeof (data) == typeof ("string")) {
                                data = $.parseJSON(data);
                            }
                            // 遍历属性
                            $.each(data, function (i, item) {
                                // 新建一个div
                                var id = "filter" + i;
                                $('#filter').after("<div id = '" + id + "' name='Dfilter'/ >")
                                // 为该div初始化值
                                id = '#' + id;
                                $(id).comboboxfilter({
                                    url: '/componentParameterDetail/filter',
                                    method: 'get',
                                    text: item.compParamName,
                                    param: {
                                        compParamId: item.compParamId
                                    },
                                    onSelect: function (itemData) {
                                        componentParamDetailIds.add(itemData.id);
                                        doSearch();
                                    },
                                    unSelect: function (itemData) {
                                        componentParamDetailIds.delete(itemData.id);
                                        doSearch();
                                    }
                                });
                            });
                        })
                    },
                    unSelect: function (itemData) {
                        $("#componentId").val("");
                        doSearch();
                        $("div[name='Dfilter']").each(function () {
                            $(this).comboboxfilter('clear');
                            $(this).remove();
                        });
                    }
                });

                $('#tt').datagrid({
                    url: "/stock/list",
                    onDblClickRow : function (index, row) {
                        $.messager.confirm('警告', '确定要直接修改库存吗', function (r) {
                            if (r) {
                                $.messager.prompt('库存数'+row.stockNum, '正在修改仓库('+row.repertoryName+')的商品('+row.wareName+')', function(r){
                                    if ((/(^[1-9]\d*$)/.test(r))){
                                        $.ajax({
                                            url: "/stock/",
                                            type: "PUT",
                                            data: {
                                                stockId: row.stockId,
                                                stockNum: r
                                            },
                                            success: function (data) {
                                                var data = data == "success" ? "修改成功" : "修改失败";
                                                $.messager.alert('结果', data, 'info');
                                                $("#tt").datagrid("reload");
                                            }
                                        });
                                    } else {
                                        $.messager.alert("警告", "请输入正整数");
                                    }
                                });
                            }
                        });
                    }
                });
            });

            function doSearch() {
                param = {};
                param.componentParameterDetailIds = "";

                if ($('#wareName').val())
                    param.wareName = $('#wareName').val();
                if ($('#wareBarCode').val())
                    param.wareBarCode = $('wareBarCode').val();
                if ($('#repertoryId').val())
                    param.repertoryId = $('#repertoryId').val();
                if ($('#componentId').val())
                    param.componentId = $('#componentId').val();
                if (componentParamDetailIds.size > 0) {
                    componentParamDetailIds.forEach(function (item) {
                        param.componentParameterDetailIds += item + " ";
                    });
                }
                param.isFuzzy = $('#isFuzzy').is(':checked');

                $('#tt').datagrid('load', param);
            }
            function doReSet() {
                $('#wareName').val("");
                $('#wareBarCode').val("");
                $("#repertoryId").combobox("clear");
                if (!$('#isFuzzy').is(':checked')) {
                    $('#isFuzzy').click();
                }
                doSearch();
            }
            //]]>
        </script>
    </div>
</table>

</body>
</html>